package net.zjitc.dao.impl;

import net.zjitc.dao.ProductDao;
import net.zjitc.domain.Product;
import net.zjitc.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by hasee on 2017/10/27.
 */
public class ProductDaoImpl implements ProductDao {
    QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
    @Override
    public List findList(int pageNum,int listLimit,String condition) {
        condition = "%"+condition+"%";
        String sql = "select * from product where pname like ? order by pid limit ?,? ";
        List<Product> list = null;
        try {
            list = (List)runner.query(sql,new BeanListHandler(Product.class),condition,((pageNum-1)*listLimit),listLimit);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int findCount(String condition) {
        condition = "%"+condition+"%";
        String sql = "select count(*) from product where pname like ?";
        int count = 0;
        try {
            Long l = (Long) runner.query(sql,new ScalarHandler(),condition);
            count = l.intValue();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public void insert(Product product) {
        String pname = product.getPname();
        String marketPrice = product.getMarketPrice();
        String shopPrice = product.getShopPrice();
        String pdesc = product.getPdesc();
        String sql = "insert into product (pname,shopPrice,pdesc) values(?,?,?)";
        try {
            runner.update(sql,pname,shopPrice,pdesc);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(String pid) {
        String sql = "delete from product where pid = ?";
        try {
            runner.update(sql,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Product findById(String pid) {
        String sql = "select * from product where pid = ?";
        Product product = null;
        try {
            product = (Product)runner.query(sql,new BeanHandler(Product.class),pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public void update(Product product) {
        String pname=product.getPname();
        String shopPrice=product.getShopPrice();
        String pdesc=product.getPdesc();
        int pid = product.getPid();
        String sql = "update product set pname=?,shopPrice=?,pdesc=? where pid=?";
        try {
            runner.update(sql,pname,shopPrice,pdesc,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
